﻿using System;
using Eassets.Layered.Domain.Asset;
using System.Data.SqlClient;
using System.Data;
using System.Collections.ObjectModel;

namespace Eassets.Data.Asset
{
    public class AssetRevaluationDetailsData
    {
        /// <summary>
        /// Inserts assetRevaluationDetails.
        /// </summary>
        /// <param name="assetRevaluationDetails"></param>
        public void AddAssetRevaluationDetails(AssetRevaluationDetails assetRevaluationDetails)
        {
            SqlCommand command = new SqlCommand("SP_AddAssetRevaluationDetails"); // Name of the SP
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, assetRevaluationDetails));
        }

        /// <summary>
        /// Updates assetRevaluationDetails.
        /// </summary>
        /// <param name="assetRevaluationDetails"></param>
        public void ModifyAssetRevaluationDetails(AssetRevaluationDetails assetRevaluationDetails)
        {
            SqlCommand command = new SqlCommand("SP_ModifyAssetRevaluationDetails"); // Name of the SP
            command.Parameters.AddWithValue("@AssetRevaluationDetailsId", assetRevaluationDetails.AssetRevalueId);
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, assetRevaluationDetails));
        }



        /// <summary>
        /// Get assetRevaluationDetails by Id.
        /// </summary>
        /// <param name="assetRevaluationDetailsId"></param>
        /// <returns></returns>
        public AssetRevaluationDetails GetByIdAssetRevaluationDetails(long assetRevaluationDetailsId)
        {
            SqlCommand command = new SqlCommand("sproc_t_c_GetAssetRevaluationDetailsById");
            command.Parameters.AddWithValue("@AssetRevaluationDetailsId", assetRevaluationDetailsId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            AssetRevaluationDetails assetRevaluationDetails = new AssetRevaluationDetails();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["AssetRevalueId"].ToString())))
                {
                    assetRevaluationDetails.AssetRevalueId = Convert.ToInt64(dataReader["AssetRevalueId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionId"].ToString())))
                {
                    assetRevaluationDetails.TransactionId = Convert.ToInt64(dataReader["TransactionId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["CompanyId"].ToString())))
                {
                    assetRevaluationDetails.CompanyId = Convert.ToInt64(dataReader["CompanyId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionNumber"].ToString())))
                {
                    assetRevaluationDetails.TransactionNumber = Convert.ToString(dataReader["TransactionNumber"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionTypeId"].ToString())))
                {
                    assetRevaluationDetails.TransactionTypeId = Convert.ToInt64(dataReader["TransactionTypeId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["DocumentCode"].ToString())))
                {
                    assetRevaluationDetails.DocumentCode = Convert.ToString(dataReader["DocumentCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    assetRevaluationDetails.AssetId = Convert.ToInt64(dataReader["AssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ItemSerialCode"].ToString())))
                {
                    assetRevaluationDetails.ItemSerialCode = Convert.ToString(dataReader["ItemSerialCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionQuantity"].ToString())))
                {
                    assetRevaluationDetails.TransactionQuantity = Convert.ToInt16(dataReader["TransactionQuantity"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["RevalueValue"].ToString())))
                {
                    assetRevaluationDetails.RevalueValue = Convert.ToDecimal(dataReader["RevalueValue"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["RevalueDate"].ToString())))
                {
                    assetRevaluationDetails.RevalueDate = Convert.ToDateTime(dataReader["RevalueDate"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["NewLifeTime"].ToString())))
                {
                    assetRevaluationDetails.NewLifeTime = Convert.ToInt16(dataReader["NewLifeTime"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OriginatedBy"].ToString())))
                {
                    assetRevaluationDetails.OriginatedBy = Convert.ToString(dataReader["OriginatedBy"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["RevalueReason"].ToString())))
                {
                    assetRevaluationDetails.RevalueReason = Convert.ToString(dataReader["RevalueReason"]);
                }
            }
            dataReader.Close();
            return assetRevaluationDetails;
        }

        /// <summary>
        /// Get assets item details by asset id
        /// </summary>
        /// <param name="assetId"></param>
        /// <returns></returns>
        public Collection<AssetItemDetails> GetFixedAssetItemDetailsByAssetId(long assetId)
        {
            Collection<AssetItemDetails> assetItemDetailss = new Collection<AssetItemDetails>();
            SqlCommand command = new SqlCommand("SP_GetFixedAssetItemDetailsForRevaluationByAssetId");
            command.Parameters.AddWithValue("@AssetId", assetId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            AssetItemDetails assetItemDetails;
            while (dataReader.Read())
            {
                assetItemDetails = new AssetItemDetails();
                if (!(String.IsNullOrEmpty(dataReader["ItemDetailsId"].ToString())))
                {
                    assetItemDetails.ItemDetailsId = Convert.ToInt64(dataReader["ItemDetailsId"]);
                }

                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    assetItemDetails.AssetId = Convert.ToInt64(dataReader["AssetId"]);
                }
                //Get Asset Object to map Asset Code to Revaluation Grid
                AssetData assetData = new AssetData();
                assetItemDetails.Assets = assetData.GetByIdAsset(assetItemDetails.AssetId);

                if (!(String.IsNullOrEmpty(dataReader["AssetStatus"].ToString())))
                {
                    assetItemDetails.AssetStatus = Convert.ToString(dataReader["AssetStatus"]);
                }

                if (!(String.IsNullOrEmpty(dataReader["AssetSerialCode"].ToString())))
                {
                    assetItemDetails.AssetSerialCode = Convert.ToString(dataReader["AssetSerialCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["PurchasedDate"].ToString())))
                {
                    assetItemDetails.PurchasedDate = Convert.ToDateTime(dataReader["PurchasedDate"]);
                }

                if (!(String.IsNullOrEmpty(dataReader["CurrentBookValue"].ToString())))
                {
                    assetItemDetails.CurrentBookValue = Convert.ToDecimal(dataReader["CurrentBookValue"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["RemainingLifeTime"].ToString())))
                {
                    assetItemDetails.RemainingLifeTime = Convert.ToInt16(dataReader["RemainingLifeTime"]);
                }

                assetItemDetailss.Add(assetItemDetails);

            }
            dataReader.Close();
            return assetItemDetailss;
        }

        /// <summary>
        /// GetAll assetRevaluationDetails.
        /// </summary>
        /// <returns></returns>
        public Collection<AssetRevaluationDetails> GetAllAssetRevaluationDetails()
        {
            Collection<AssetRevaluationDetails> assetRevaluationDetailss = new Collection<AssetRevaluationDetails>();
            SqlCommand command = new SqlCommand("sproc_t_l_GetAllAssetRevaluationDetailss");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            AssetRevaluationDetails assetRevaluationDetails;
            while (dataReader.Read())
            {
                assetRevaluationDetails = new AssetRevaluationDetails();
                if (!(String.IsNullOrEmpty(dataReader["AssetRevalueId"].ToString())))
                {
                    assetRevaluationDetails.AssetRevalueId = Convert.ToInt64(dataReader["AssetRevalueId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionId"].ToString())))
                {
                    assetRevaluationDetails.TransactionId = Convert.ToInt64(dataReader["TransactionId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["CompanyId"].ToString())))
                {
                    assetRevaluationDetails.CompanyId = Convert.ToInt64(dataReader["CompanyId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionNumber"].ToString())))
                {
                    assetRevaluationDetails.TransactionNumber = Convert.ToString(dataReader["TransactionNumber"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionTypeId"].ToString())))
                {
                    assetRevaluationDetails.TransactionTypeId = Convert.ToInt64(dataReader["TransactionTypeId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["DocumentCode"].ToString())))
                {
                    assetRevaluationDetails.DocumentCode = Convert.ToString(dataReader["DocumentCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    assetRevaluationDetails.AssetId = Convert.ToInt64(dataReader["AssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ItemSerialCode"].ToString())))
                {
                    assetRevaluationDetails.ItemSerialCode = Convert.ToString(dataReader["ItemSerialCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionQuantity"].ToString())))
                {
                    assetRevaluationDetails.TransactionQuantity = Convert.ToInt16(dataReader["TransactionQuantity"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["RevalueValue"].ToString())))
                {
                    assetRevaluationDetails.RevalueValue = Convert.ToDecimal(dataReader["RevalueValue"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["RevalueDate"].ToString())))
                {
                    assetRevaluationDetails.RevalueDate = Convert.ToDateTime(dataReader["RevalueDate"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["NewLifeTime"].ToString())))
                {
                    assetRevaluationDetails.NewLifeTime = Convert.ToInt16(dataReader["NewLifeTime"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OriginatedBy"].ToString())))
                {
                    assetRevaluationDetails.OriginatedBy = Convert.ToString(dataReader["OriginatedBy"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["RevalueReason"].ToString())))
                {
                    assetRevaluationDetails.RevalueReason = Convert.ToString(dataReader["RevalueReason"]);
                }

                assetRevaluationDetailss.Add(assetRevaluationDetails);
            }
            dataReader.Close();
            return assetRevaluationDetailss;
        }

        public SqlCommand ConstructCommand(SqlCommand command, AssetRevaluationDetails assetRevaluationDetails)
        {
            command.CommandType = CommandType.StoredProcedure;

            try
            {
                command.Parameters.AddWithValue("@TransactionId", assetRevaluationDetails.TransactionId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@CompanyId", assetRevaluationDetails.CompanyId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@AssetId", assetRevaluationDetails.AssetId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@ItemSerialCode", assetRevaluationDetails.ItemSerialCode);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@RevalueValue", assetRevaluationDetails.RevalueValue);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@RevalueDate", assetRevaluationDetails.RevalueDate);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@NewLifeTime", assetRevaluationDetails.NewLifeTime);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@OriginatedBy", assetRevaluationDetails.OriginatedBy);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@RevalueReason", assetRevaluationDetails.RevalueReason);
            }
            catch (Exception)
            { }
            //try
            //{
            //    command.Parameters.AddWithValue("@RevalueValue", assetRevaluationDetails.RevalueValue);
            //}
            //catch (Exception)
            //{ }
            //try
            //{
            //    command.Parameters.AddWithValue("@RevalueDate", assetRevaluationDetails.RevalueDate);
            //}
            //catch (Exception)
            //{ }
            //try
            //{
            //    command.Parameters.AddWithValue("@NewLifeTime", assetRevaluationDetails.NewLifeTime);
            //}
            //catch (Exception)
            //{ }
            //try
            //{
            //    command.Parameters.AddWithValue("@OriginatedBy", assetRevaluationDetails.OriginatedBy);
            //}
            //catch (Exception)
            //{ }
            //try
            //{
            //    command.Parameters.AddWithValue("@RevalueReason", assetRevaluationDetails.RevalueReason);
            //}
            //catch (Exception)
            //{ }
            return command;
        }


    }
}



